package org.hk.kg.finance.costype.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hk.kg.db.DBServiceProxy;
import org.hk.kg.finance.FinanceConstants;
import org.hk.kg.finance.costype.service.CostypeService;
import org.hk.kg.finance.costype.service.ICostypeService;
import org.hk.kg.ui.ParamsStack;
import org.hk.kg.utils.Constants;
import org.hk.kg.utils.StringUtils;
import org.hk.kg.web.BaseAction;

public class CostTypeAction extends BaseAction {
	private ICostypeService costypeService = DBServiceProxy.getProxyClz(CostypeService.class);
	private List<Map> qry_rlt;
	public String getCostTypes()
	{
		StringBuffer buffer = new StringBuffer();
		qry_rlt = costypeService.queryCostTypes();
		for(Map rlt : qry_rlt)
		{
			String isActive = "" + rlt.get("active");
			String checked = "";
			if(isActive.equals("1"))
			{
				checked = " checked=\"checked\" ";
			}
			buffer.append("<tr>")
				.append("<td>").append("<input name=\"costTypeId\" text=\"false\" type=\"hidden\" value=\"").append(rlt.get("costTypeId")).append("\"/>")
				.append("<input name=\"active\" value=\"true\" type=\"checkbox\"").append(checked).append("/>").append("</td>")
				.append("<td>").append("<input type=\"text\" name=\"costName\" value=\"").append(rlt.get("costTypeName")).append("\"/></td>")
				.append("<td>").append(getCostypeKindOptions(""+rlt.get("costTypeKind"))).append("</td>")
				.append("<td>").append("<a href=\"#deleteCostype?del_Id=").append(rlt.get("costTypeId")).append("\">删除").append("</a></td>")
				.append("</tr>");
		}
		return buffer.toString();
	}
	
	public String deleteCostype(ParamsStack stack)
	{
		String costypeIdStr = stack.getValue("del_Id");
		if(!StringUtils.isEmpty(costypeIdStr))
		{
			if(costypeService.deleteCostType(Integer.parseInt(costypeIdStr)))
			{
				return "cost_type";
			}
			else
			{
				return Constants.COMMON_FAIL_ACTION;
			}
		}
		return None;
	}
	
	public String turn2Create(ParamsStack stack)
	{
		return "new_cost_type";
	}
	
	public String doSaveAll(ParamsStack stack)
	{

		List<Map> upts = getDirties(stack);
		if(upts.isEmpty())
		{
			return None;
		}
		if(costypeService.updateCostTypes(upts))
		{
			return "cost_type";
		}
		else
		{
			return Constants.COMMON_FAIL_ACTION;
		}
	}
	
	private String getCostypeKindOptions(String choosen)
	{
		Map<String, String> clzTypes = Constants.CONFIG.getGroup(FinanceConstants.COST_TYPE_KINDS);
		StringBuffer buffer = new StringBuffer("<select name=\"costypeKind\">");
		for(Entry<String, String> entry : clzTypes.entrySet())
		{
			String selected = "";
			if(entry.getKey().equals(choosen))
			{
				selected = " selected=\"selected\" ";
			}
			buffer.append("<option value=\"").append(entry.getKey()).append("\"").append(selected)
				.append(">").append(entry.getValue()).append("</option>\n");
		}
		return buffer.append("</select>").toString();
	}
	
	private List<Map> getDirties(ParamsStack stack)
	{
		//获取修改过的数据
		Object costTypeIds[] = stack.getValues("costTypeId"),
			actives[] = stack.getValues("active", true),
			costNames[] = stack.getValues("costName"),
			costypeKinds[] = stack.getValues("costypeKind");
		List<Map> rlt = new ArrayList<Map>();
		for(int idx = 0 ; idx < costTypeIds.length;idx++)
		{
			Object costTypeId = costTypeIds[idx],
								active = "true".equals(actives[idx])?"1":"0",
								costName = costNames[idx],
								costTypeKind = costypeKinds[idx];
			for(Map m : qry_rlt)
			{
				if((""+m.get("costTypeId")).equals(costTypeId))
				{
					Object active_hold = "" + m.get("active"),
						costName_hold = m.get("costTypeName"),
						costypeKind_hold = "" + m.get("costTypeKind");
					if(!active_hold.equals(active)
							||!costName_hold.equals(costName)
							||!costypeKind_hold.equals(costTypeKind))
					{
						logger.info("changed");
						Map changed = new HashMap();
						changed.put("costTypeId", costTypeId);
						changed.put("active", active);
						changed.put("costName", costName);
						changed.put("costTypeKind", costTypeKind);
						rlt.add(changed);
					}
					
				}
			}
		}

		return rlt;
	}
}
